package cn.ekuma.epos.crm.dialog;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;

import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.swing.dialog.AbstractKeyBoardJDialog;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;

import com.openbravo.bean.crm.CustomerStoreValueCard;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.format.Formats;

import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.JStoreCardValueOpPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class JStoreValueCardTranforOutDialog  extends AbstractKeyBoardJDialog{

	private double maxValue;
	JStoreCardValueOpPanel storeCardValueOp;
	private JTextField txtMaxValue;
	/**
	 * @wbp.parser.constructor
	 */
	public JStoreValueCardTranforOutDialog(Dialog parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}


	public JStoreValueCardTranforOutDialog(Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}
	
	public static JStoreValueCardTranforOutDialog newJDialog(Component superComp,I_DataLogicSales dSales,double number) {
        Window window = getWindow(superComp);
        JStoreValueCardTranforOutDialog mydialog;
        if (window instanceof Frame) {
            mydialog = new JStoreValueCardTranforOutDialog((Frame) window, true);
        } else {
            mydialog = new JStoreValueCardTranforOutDialog((Dialog) window, true);
        }
        mydialog.init(dSales,number);
        mydialog.applyComponentOrientation(superComp.getComponentOrientation());
        return mydialog;
    }
	
	
	private void init(I_DataLogicSales dSales,double maxValue) {
		this.maxValue=maxValue;
		storeCardValueOp=new JStoreCardValueOpPanel(dSales);
		getContentPane().add(storeCardValueOp, BorderLayout.CENTER);
		storeCardValueOp.activate();
		txtMaxValue.setText(Formats.CURRENCY.formatValue(maxValue));
	}
	
	private void initComponents() {
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		JButton button = new JButton("OK");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				enterKeyProc();
			}
		});
		
		JLabel lblNewLabel = new JLabel(AppLocal.getIntString("label.CustomerStoreValueCard.curLimit"));
		panel.add(lblNewLabel);
		
		txtMaxValue = new JTextField();
		txtMaxValue.setEditable(false);
		panel.add(txtMaxValue);
		txtMaxValue.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("                          ");
		panel.add(lblNewLabel_1);
		panel.add(button);
		
		JButton button_1 = new JButton("Cancel");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				escapeKeyProc();
			}
		});
		panel.add(button_1);
		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-580)/2, (screenSize.height-400)/2, 580, 400);
		
	}
	
	@Override
	protected void enterKeyProc() {
		if(storeCardValueOp.getStoreCard()==null)
			dispose();
		if(storeCardValueOp.getStoreCard()!=null&&storeCardValueOp.getM_dPaid()!=null&&storeCardValueOp.getM_dPaid()<=maxValue){
			dispose();
		}	
	}

	@Override
	protected void escapeKeyProc() {
		storeCardValueOp.reset();
		dispose();
		
	}

	@Override
	protected void upKeyProc() {

	}

	@Override
	protected void downKeyProc() {

	}

	public Double getM_dPaid() {
		return storeCardValueOp.getM_dPaid();
	}

	public CustomerStoreValueCard getStoreCard() {
		return storeCardValueOp.getStoreCard();
	}
	
}
